package org.jypj.dev.controller;

import org.jypj.dev.model.Code;
import org.jypj.dev.model.CodeData;
import org.jypj.dev.service.ICodeDataService;
import org.jypj.dev.service.ICodeService;
import org.jypj.dev.util.Page;
import org.jypj.dev.util.StringUtil;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Code控制器
 *
 * @author ChenYu
 */
@Controller
@RequestMapping("codeController")
public class CodeController {

    @Resource
    private ICodeService codeService;

    @Resource
    private ICodeDataService codeDataService;

    @RequestMapping(value = "toCodeList.action")
    public String toCodeList() {

        return "back/sys/dic_main";
    }

    /**
     * 跳转到code form页面
     *
     * @return
     * @throws UnsupportedEncodingException
     */
    @RequestMapping(value = "toCodeForm.action")
    public String toCodeForm(String id, HttpServletRequest request) {
        if (StringUtil.isNotEmpty(id)) {
            CodeData cd = codeDataService.selectCodeDataById(id);
            request.setAttribute("cd", cd);
        }
        return "back/sys/dic_form";
    }

    /**
     * 查询所有数据接口
     *
     * @return code =-1 表示有异常 code=-2 表示没有数据 datas 菜单数据
     */
    @RequestMapping("selectAllCode.action")
    @ResponseBody
    public Map<String, Object> selectAllCode() {
        Map<String, Object> map = new HashMap<>();
        int code = 0;
        try {
            List<Code> codes = codeService.selectAllByCode(new Code());
            map.put("datas", codes);
        } catch (Exception e) {
            code = -1;
        }
        map.put("code", code);
        return map;
    }

    /**
     * 查询所有子字典
     *
     * @return
     */
    @RequestMapping(value = "selectAllCodeData.action")
    @ResponseBody
    public Map<String, Object> selectAllCodeData(String sysCode) {
        Map<String, Object> map = new HashMap<>();
        int code = 0;
        Map<String, Object> queryMap = new HashMap<>();
        queryMap.put("code", sysCode);
        try {
            List<CodeData> cds = codeDataService.selectAllByMap(queryMap);
            if (cds == null || cds.size() <= 0) {
                code = -2;
            }
            map.put("data", cds);
        } catch (Exception e) {
            code = -1;
        }
        map.put("code", code);
        return map;
    }

    /**
     * 分页查询所有子字典
     *
     * @return
     */
    @RequestMapping(value = "selectOnePageCodeData.action")
    @ResponseBody
    public Page selectOnePageCodeData(String codeId, Page page, String searchKey) {
        Map<String, Object> queryMap = new HashMap<>();
        queryMap.put("codeId", codeId);
        queryMap.put("searchKey", searchKey);
        page = codeDataService.selectOnePageByMap(page, queryMap);
        return page;
    }

    /**
     * 保存字典对象
     *
     * @param codeData
     * @return code -2 表示字典值重复 -1表示保存失败
     */
    @RequestMapping(value = "saveOrUpdateCodeData.action")
    @ResponseBody
    public Map<String, Object> saveOrUpdateCodeData(CodeData codeData) {
        int code = 0;
        Map<String, Object> map = new HashMap<>();
        try {
            if (StringUtil.isNotEmpty(codeData.getId())) {
                codeDataService.updateCodeData(codeData);
            } else {
                codeDataService.saveCodeData(codeData);
            }
        } catch (DuplicateKeyException e) {
            code = -2;
        } catch (Exception e) {
            code = -1;
        }
        map.put("code", code);
        return map;
    }

    /**
     * 删除字典
     *
     * @param codeData
     * @return
     */
    @RequestMapping(value = "deleteCodeData.action")
    @ResponseBody
    public Map<String, Object> deleteCodeData(String id) {
        int code = 0;
        Map<String, Object> map = new HashMap<>();
        try {
            codeDataService.deleteCodeDataById(id);
        } catch (Exception e) {
            code = -1;
        }
        map.put("code", code);
        return map;
    }

    /**
     * 更新
     *
     * @param codeData
     * @return
     */
    @RequestMapping(value = "updateCodeData.action")
    @ResponseBody
    public Map<String, Object> updateCodeData(CodeData codeData) {
        int code = 0;
        Map<String, Object> map = new HashMap<>();
        try {
            codeDataService.updateCodeData(codeData);
        } catch (DuplicateKeyException e) {
            code = -2;
        } catch (Exception e) {
            code = -1;
        }
        map.put("code", code);
        return map;
    }
}